﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RealtorsPortal.Data_Access_Layer;
using RealtorsPortal.Entities;

namespace RealtorsPortal.AdminCP
{
    public partial class Login : System.Web.UI.Page
    {
        string returnURL = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            this.IsLogged();
            if (Request.QueryString["returnUrl"] != null)
            {
                returnURL = Request.QueryString["returnUrl"].ToString();
            }
        }


        private void IsLogged()
        {
            if (Session["adminLogin"] != null)
            {
                Response.Redirect("~/AdminCP/Default.aspx");
            }
            else if (Request.Cookies["AdminLogin"] != null)
            {
                HttpCookie ckLogin = Request.Cookies["AdminLogin"];
                Session["adminLogin"] = ckLogin["adminLogin"].ToString();
                Session["adminId"] = ckLogin["adminId"].ToString();
                Session["adminRole"] = ckLogin["adminRole"].ToString();
                Response.Redirect("~/AdminCP/Default.aspx");
            }

        }


        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string user = txtUsername.Text.Trim();
            string pass = Utilities.GetMD5HashData(txtPassword.Text);
            bool isRemember = chkRemember.Checked;
            bool isAdmin = chkIsAdmin.Checked;
            string result = "";
            AdministratorDAL amd = new AdministratorDAL();
            AccountDAL ad = new AccountDAL();
            if (isAdmin)
            {
                result = amd.CheckAdminLogin(user, pass);
            }
            else {
                result = ad.CheckLogin(user, pass);
            }
           
            
            switch (result)
            {
                case "-1":
                    lblMessage.Text = "Username doesn't exist!";
                    break;
                case "-2":
                    lblMessage.Text = "Wrong password!";
                    break;
                default:
                    int adminId =0;
                    if (isAdmin)
                    {
                        adminId =amd.GetAdminIdByUsername(user);
                    }
                    else
                    {
                        adminId = ad.GetIdByUsername(user);
                    }
                    Session["adminId"] = adminId;
                    Session["adminLogin"] = user;
                    Session["adminRole"] = result;
                    if (isRemember)
                    {
                        HttpCookie ckLogin = new HttpCookie("AdminLogin");
                        ckLogin["adminId"] = adminId.ToString();
                        ckLogin["adminLogin"] = user;
                        ckLogin["adminRole"] = result;
                        ckLogin.Expires = DateTime.Now.AddDays(3d);
                        Response.Cookies.Add(ckLogin);
                    }
                    if (returnURL != "")
                    {
                        Response.Redirect(returnURL);
                    }
                    else
                    {
                        Response.Redirect("~/AdminCP/Default.aspx");
                    }
                    break;
            }
        }
    }
}